package vcp.controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import db.parking.beans.Parking;
import db.parking.beans.ParkingLot;
import vcp.application.UserManagement;
import vcp.common.AbstractController;
import vcp.common.IController;
import vcp.gui.forms.validators.NumericValidator;
import vcp.gui.panels.pnlCellPane;
import vcp.models.SaveParkingModel;
import vcp.views.SaveParkingView;

/**
 * Save Parking Controller
 *
 */
public class SaveParkingController extends AbstractController implements
		ActionListener, AncestorListener, MouseListener {

	private SaveParkingModel saveParkingModel;
	private SaveParkingView saveParkingView;

	// SaveParkingView view;
	

	public void addModel(Object model) {
		// TODO Auto-generated method stub
		this.saveParkingModel = (SaveParkingModel) model;
	}

	public void addView(Object view) {
		this.saveParkingView = (SaveParkingView) view;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String action = e.getActionCommand();
		if (action.equals("levelChoice")) {
			JComboBox<?> combo = (JComboBox<?>) e.getSource();
			if (!combo.getSelectedItem().equals(null)) {
				saveParkingModel.switchLevel(Integer.parseInt(combo
						.getSelectedItem().toString()));

			}
		
		}
	}

	@Override
	public void ancestorAdded(AncestorEvent arg0) {		
		saveParkingModel.getParkingObject();
	}

	@Override
	public void ancestorMoved(AncestorEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void ancestorRemoved(AncestorEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent arg) {

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg) {
		pnlCellPane cell = (pnlCellPane) arg.getSource();
		switch (cell.getState()) {
		case 1: {
			JOptionPane.showMessageDialog(null, "Taken...");
			break;
		}
		case 2:
			JOptionPane.showMessageDialog(null, "Serviced...");
			break;
		case 3:
			JOptionPane.showMessageDialog(null, "Preserved...");
			break;
		default: {
			String userPrivilege = UserManagement.getInstance().getEmployee()
					.getType();
			switch (userPrivilege) {
			case "CSWorker":
				saveParking(cell);
				break;
			case "PWorker": {
				Object[] options = { "Save Parking", "Disable Parking",
						"Cancel" };
				int dialogresult = JOptionPane
						.showOptionDialog(null, "Set parking status",
								"Confirmation", JOptionPane.DEFAULT_OPTION,
								JOptionPane.QUESTION_MESSAGE, null, options,
								options[2]);
				switch (dialogresult) {
				case 0:
					saveParking(cell);
					break;
				case 1:
					disableParking(cell);
					break;
				}
				break;
			}
			}
			break;
		}
		}
	}
	
	
	
	private void disableParking(pnlCellPane cell) {
		if(cell!=null){
		    cell.setBackground(Color.red);
		    cell.setState(2);
			Parking parking = new Parking();
			parking.setCarNo("0");
			ParkingLot pl = cell.getPl();
			pl.setLocation(cell.getPpX(), cell.getPpY(),
					pl.getFlorNum() - 1, cell.getState());
			parking.setFloorNo(pl.getFlorNum() - 1);
			parking.setPositionX(cell.getPpX());
			parking.setPositionY(cell.getPpY());
			parking.setStatus(cell.getState());
			parking.setLotNo(pl.getLotID());	
			
			ArrayList<Object> obj = new ArrayList<Object>();
			obj.add(parking);
			obj.add(UserManagement.getInstance().getEmployee().getEmpID());
			
			saveParkingModel.SavePlState(obj);	
			JOptionPane.showMessageDialog(null, "Parking disabled!");
		}
	}

	private void saveParking(pnlCellPane cell){
		if(cell!=null){
		JTextField carnum = new JTextField();
		int okCxl = JOptionPane.showConfirmDialog(null, carnum,
				"Enter your car number", JOptionPane.OK_CANCEL_OPTION);

		if (okCxl == JOptionPane.OK_OPTION) {
			NumericValidator validator = new NumericValidator("\\d+");
			if (!carnum.getText().isEmpty() && validator.validate(carnum.getText())) {
				cell.setState(3);
				Parking parking = new Parking();
				parking.setCarNo(carnum.getText());
				cell.setBackground(Color.blue);
				ParkingLot pl = cell.getPl();
				pl.setLocation(cell.getPpX(), cell.getPpY(),
						pl.getFlorNum() - 1, cell.getState());
				parking.setFloorNo(pl.getFlorNum() - 1);
				parking.setPositionX(cell.getPpX());
				parking.setPositionY(cell.getPpY());
				parking.setStatus(cell.getState());
				parking.setLotNo(pl.getLotID());	
				
				ArrayList<Object> obj = new ArrayList<Object>();
				obj.add(parking);
				obj.add(UserManagement.getInstance().getEmployee().getEmpID());
				saveParkingModel.SavePlState(obj);	
				JOptionPane.showMessageDialog(null, "Car #" + carnum.getText()
						+ " placed sucessfully!");
			} else {
				JOptionPane.showMessageDialog(null, "Wrong car number ... ",
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	  }
	}	

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}